//记录选择的是哪个条件选择，-1默认为不限
var searchList = [-1, -1, -1, -1, -1, -1];

//分页变量
var total_page=1;       //总页数
var current_page=1;     //当前页

$(document).ready(function(){
    //初始化请求数据
    requestData();

    function requestData(){
        var institution_name = $('#institution_name').val().trim();
        var product_name = $('#product_name').val().trim();

        var productsItem = $("#products-item").html();

        //console.log(searchList);
        $.ajax({
            type:"POST",
            url:'http://localhost:8888/bank/getFinacialProduct',
            data: {
                pageNum : current_page,
                pageSize : 10, //页容量，默认为10
                bankName : institution_name,
                productName : product_name,
                institutionType : searchList[0]==-1?null:searchList[0],    //机构类型
                loanPeriod : searchList[1]==-1?null:searchList[1],         //贷款期限
                guaranteeMode : searchList[2]==-1?null:searchList[2],
                internetLoanType : searchList[3]==-1?null:searchList[3],
                productProperties : searchList[4]==-1?null:searchList[4],
                loanLimit : searchList[5]==-1?null:searchList[5]
            },
            success:function (data) {
                if (data.data == null) {
                    alert(data.meta.message);
                    $("#numText").text(0);
                    $(".products-list").html("<div class='products-item' id=products-item>"+productsItem+"</div>");
                }else{
                    if(data.data.Total!=null){
                        total_page=data.data["Total Page Num"];
                        $("#numText").text(data.data.Total);
                        //console.log(current_page,total_page);
                        //alert(data.meta.message);
                        $(".products-list").html("<div class='products-item' id=products-item>"+productsItem+"</div>")
                        $.each(data.data["governmentPolicies List"],function(i,val){
                            $(".products-list").append("<div class='products-item' id=products-item"+i+">"+productsItem+"</div>");
                            $("#products-item"+i+" a img").attr('src','http://localhost:8888/imagepath/'+val.bankName+'.png');
                            $("#products-item"+i+" em:eq(0)").html(val.bankName+"-"+val.productName);
                            $("#products-item"+i+" em:eq(1)").html(val.rateLowerLimit+"%-"+val.rateUpperLimit+"%");
                            var proloanLimit = val.loanLimit;
                            if (proloanLimit == "0") {proloanLimit="100万及以下";}
                            if (proloanLimit == "1") {proloanLimit="200万及以下";}
                            if (proloanLimit == "2") {proloanLimit="300万及以下";}
                            if (proloanLimit == "3") {proloanLimit="500万及以下";}
                            if (proloanLimit == "4") {proloanLimit="1000万及以下";}
                            if (proloanLimit == "5") {proloanLimit="1000万以上";}
                            $("#products-item"+i+" em:eq(2)").html(proloanLimit);
                            var proguaranteeMode = val.guaranteeMode;
                            if (proguaranteeMode == "0") { proguaranteeMode = "抵押";}
                            if (proguaranteeMode == "1") { proguaranteeMode = "信保基金";}
                            if (proguaranteeMode == "2") { proguaranteeMode = "一般保证";}
                            if (proguaranteeMode == "3") { proguaranteeMode = "信用";}
                            if (proguaranteeMode == "4") { proguaranteeMode = "实际控制人夫妇提供个人连带担保";}
                            $("#products-item"+i+" em:eq(3)").html(proguaranteeMode);
                            var loanPeriod = val.loanPeriod;
                            if (loanPeriod == "0") { loanPeriod = "3个月及以下";}
                            if (loanPeriod == "1") { loanPeriod = "6个月及以下";}
                            if (loanPeriod == "2") { loanPeriod = "12个月及以下";}
                            if (loanPeriod == "3") { loanPeriod = "36个月及以下";}
                            if (loanPeriod == "4") { loanPeriod = "36个月以上";}
                            $("#products-item"+i+" em:eq(4)").html(loanPeriod);
                        });
                        $(".paginator em:eq(0)").html(total_page);
                        $(".paginator em:eq(1)").html(current_page);

                        $(".detail").click(function (e) {
                            var val=$(e.target).parent().parent().parent().prev().children().text();
                            var arr=val.split("-");
                            //console.log(val);
                            sessionStorage.setItem("detailBankName",arr[0]);
                            sessionStorage.setItem("detailProductName",arr[1]);
                            sessionStorage.setItem("pageNum",current_page);
                            window.location.href="productsDetail_1.html";

                        })
                    }
                }
                $("#products-item").hide();
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                alert(XMLHttpRequest.status);
                alert(XMLHttpRequest.readyState);
                alert(textStatus);
            }
        })

    }

    $('.institution').click(function(){
        $('.institution').removeClass('current');
        $(this).addClass('current');
        var index=$('.institution').index(this);  //获取当前元素的下标,对应数据库类型
        searchList[0] = index-1;
        //searchList[0] = $('.institution').index(this)-1;  //获取当前元素的下标,对应数据库类型

        // updateList();

    });

    $('.period').click(function(){
        $('.period').removeClass('current');
        $(this).addClass('current');

        var index=$('.period').index(this);  //获取当前元素的下标,对应数据库类型
        searchList[1] = index-1;

        // updateList();
    });

    $('.way').click(function(){
        $('.way').removeClass('current');
        $(this).addClass('current');

        var index=$('.way').index(this);  //获取当前元素的下标,对应数据库类型
        searchList[2] = index-1;

        // updateList();
    });

    $('.connect').click(function(){
        $('.connect').removeClass('current');
        $(this).addClass('current');

        var index=$('.connect').index(this);  //获取当前元素的下标,对应数据库类型
        searchList[3] = index-1;

        // updateList();
    });

    $('.policy').click(function(){
        $('.policy').removeClass('current');
        $(this).addClass('current');

        var index=$('.policy').index(this);  //获取当前元素的下标,对应数据库类型
        searchList[4] = index-1;

        // updateList();
    });

    $('.limit').click(function(){
        $('.limit').removeClass('current');
        $(this).addClass('current');

        var index=$('.limit').index(this);  //获取当前元素的下标-1,对应数据库类型
        searchList[5] = index-1;

        // updateList();
    });

    $('#search-btn').click(function(){

        requestData();
    });


    //上一页 判断上一页是否存在,若存在将上一页的页码发给后端请求上一页数据
    $('.page-prev').click(function () {
        //判断上一页存在
        if(current_page>1){
            current_page--;
            requestData();
        }
        else{
            alert("没有该页数据");
        }
    });
    //下一页 判断下一页是否存在，若存在将下一页页码发给后端请求下一页数据
    $('.page-next').click(function () {
        //判断下一页存在
        if(current_page<total_page){
            current_page++;
            requestData();
        }
        else{
            alert("没有该页数据");
        }
    });

});